package org.aiforum.frountend.service;

import java.util.Date;
import java.util.List;
import javax.transaction.Transactional;
import javax.transaction.Transactional.TxType;

import org.aiforum.domain.Topic;
import org.aiforum.frountend.common.Pageing;
import org.aiforum.frountend.common.Response;
import org.aiforum.frountend.dao.TopicDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 联系我们业务实现
 * 
 * @author VIC
 *
 */
@Service("topicService")
@Transactional(value = TxType.REQUIRED)
public class TopicServiceImpl implements TopicService {

	@Autowired
	private TopicDao topicDao;

	@Override
	public Response post(Topic entity) {
		// 设置状态和创建日期
		entity.setState(1);
		entity.setCreateDate(new Date());
		topicDao.persist(entity);
		Response response = new Response();
		response.setState(true);
		response.setData("<script>window.location.href='message?message=Send success.';</script>");
		response.setMessage("Send success.");
		return response;
	}
	
	@Override
	public Pageing queryPageingByPlateId(Pageing pageing) {
		// 初始化查询总页数SQL
		pageing.setQueryLineHql("select count(t.topicId) from Topic t where t.plateId=" + pageing.getKeyword());
		// 查询并计算总页数
		queryTotal(pageing);
		// 初始化查询HQL
		pageing.setQuaryDataHql("from Topic t where t.plateId=" + pageing.getKeyword());
		// 从数据库中查询
		queryDatabase(pageing);
		return pageing;
	}

	@Override
	public Pageing queryPageingLikeTitle(Pageing pageing) {
		String temp = "'%" + pageing.getKeyword() + "%'";
		// 初始化查询总页数HQL
		pageing.setQueryLineHql("select count(t.topicId) from Topic t where t.title like " + temp);
		// 查询计算总页数
		queryTotal(pageing);
		// 初始化模糊查询参数
		pageing.setQuaryDataHql("from Topic t where t.title like " + temp);
		// 从数据库中查询数据
		queryDatabase(pageing);
		return pageing;
	}

	/**
	 * 从数据库中查询数据
	 * @param pageing
	 */
	private void queryDatabase(Pageing pageing) {
		// 否则查询数据库,然后让如缓存
		pageing.calculationFirstIndex();
		pageing.calculationMaxIndex();
		List<Topic> topics = topicDao.queryPageing(pageing.getQuaryDataHql(),pageing.getFirstIndex(), pageing.getMaxIndex());
		boolean result = null != topics && topics.size() > 0;
		if (result) {
			pageing.setState(result);
			pageing.setTopics(topics);
		} else {
			pageing.setMessage("not result.");
		}
	}

	/**
	 * 查询计算总页数
	 * 
	 * @param pageing
	 */
	private void queryTotal(Pageing pageing) {
		// 如果总页数小于等于0
		if (pageing.getTotal() <= 0) {
			// 查询总记录数
			Object result = topicDao.queryLine(pageing.getQueryLineHql());
			if (null != result) {
				int line = ((Long) result).intValue();
				// 记录总记录数
				pageing.setLine(line);
				// 计算总页数
				pageing.calculationTotal();
			}
		}
	}

	@Override
	public Topic findById(int topicId) {
		return topicDao.findById(topicId);
	}

	@Override
	public Pageing queryPageingAll(Pageing pageing) {
		// 初始化查询总页数HQL
		pageing.setQueryLineHql("select count(t.topicId) from Topic t");
		// 查询计算总页数
		queryTotal(pageing);
		// 初始化模糊查询参数
		pageing.setQuaryDataHql("from Topic t order by t.createDate desc");
		// 从数据库中查询数据
		queryDatabase(pageing);
		return pageing;
	}
}
